﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Models;
using VietPremier.Repository;
using System.Text;

namespace VietPremier.Controllers
{
    public class PromotionController : Controller
    {

        public ActionResult Index()
        {
            using(dbVietPremierDataContext db = new dbVietPremierDataContext()){
                GetPromotionModel model = new GetPromotionModel();

                EntityPromotion promotion = db.EntityPromotions.FirstOrDefault(o => o.Show == "Show");
                if (promotion != null)
                {
                    model.promotion.Name = promotion.Name;
                    model.promotion.Description = promotion.Description;
                    model.promotion.Itinerary = promotion.Itinerary;
                    model.promotion.Note = promotion.Note;
                    //SEO
                    model.promotion.Titletag = promotion.Titletag;
                    model.promotion.Meta_description = promotion.Meta_description;
                    model.promotion.Meta_keyword = promotion.Meta_keyword;

                    foreach (var listPromotion in db.EntityPromotions.Where(o => o.Show == "Hidden").Take(4))
                    {
                        model.listPromotion.Add(new Promotion()
                        {
                            Id = listPromotion.Id,
                            Name = listPromotion.Name,
                            Image = listPromotion.Image,
                            Description = listPromotion.Description
                        });
                    }
                    //using loop to get all Style in EntityStyle
                    foreach (var style in db.EntityStyles)
                    {
                        model.listStyle.Add(new Style()
                        {
                            Id = style.Id,
                            Name = style.Name
                        });
                    }
                    model.category = new SelectList(model.listStyle.ToArray(), "Name", "Name");

                    return View(model);
                }
                return View();
            }
        }

        [HttpPost]
        public ActionResult Index(GetPromotionModel model)
        {
            if (ModelState.IsValid)
            {
                using (dbVietPremierDataContext db = new dbVietPremierDataContext())
                {
                        StringBuilder mailBody = new StringBuilder();
                        mailBody.AppendFormat("<h1>Promotion Program Information</h1>");
                        mailBody.AppendFormat("<table><tr><td style='width:200px'>Promotion program :</td><td>" + model.promotion.Name + "</td></tr>");
                        mailBody.AppendFormat("<tr style='height:20px'></tr> ");
                        mailBody.AppendFormat("<tr><td>Họ tên:</td><td>" + model.Gender + " " + model.FullName + "</td></tr>");
                        mailBody.AppendFormat("<tr><td>Đơn vị/ Doanh nghiệp:</td><td>" + model.Supplier + "</td></tr>");
                        mailBody.AppendFormat("<tr><td>Nơi đến:</td><td>" + model.Destination + "</td></tr>");
                        mailBody.AppendFormat("<tr><td>Quốc tịch:</td><td>" + model.Nationality + "</td></tr>");
                        mailBody.AppendFormat("<tr><td>Điện thoại:</td><td>" + model.Telephone + "</td></tr>");
                        mailBody.AppendFormat("<tr><td>Email:</td><td>" + model.Email + "</td></tr>");
                        mailBody.AppendFormat("<tr><td>Số lượng người lớn:</td><td>" + model.NumberofAdults + "</td></tr>");
                        mailBody.AppendFormat("<tr><td>Số lượng trẻ em:</td><td>" + model.NumberofChildren + "</td></tr>");
                        mailBody.AppendFormat("<tr><td>Ngày đi:</td><td>" + model.DateFrom + "</td></tr>");
                        mailBody.AppendFormat("<tr><td>Ngày về:</td><td>" + model.DateTo + "</td></tr>");
                        mailBody.AppendFormat("<tr><td>Chuẩn khách sạn:</td><td>" + model.Place + "</td></tr>");
                        mailBody.AppendFormat("<tr><td>Yêu cầu đặc biệt khác:</td><td>" + model.SpecialRequest + "</td></tr></table>");
                        mailBody.AppendFormat("<br></br>");
                        mailBody.AppendFormat("<p>Email được gửi từ một email ảo, No reply</p>");
                        
                        try
                        {
                            MailHelper.SendMailMessage("noreply@khainguyenepco.vn", "sales@vietnampremier.com", "hmduc@khainguyenepco.vn", "khoa@vietnampremier.com", "subject", mailBody.ToString(), "smtp.gmail.com", true, "noreply@khainguyenepco.vn", "kh@inguyen");
                            EntityFeedBack feedback = new EntityFeedBack();
                            feedback.Email = model.Email;
                            feedback.DateCreate = DateTime.Now;
                            feedback.Body = mailBody.ToString();
                            feedback.Page = "Promotion";
                            db.EntityFeedBacks.InsertOnSubmit(feedback);
                            db.SubmitChanges();

                            TempData["Message"] = "Success";
                        }
                        catch
                        {
                            TempData["Message"] = "ErrorMessage";
                        }

                        model = new GetPromotionModel();
                        EntityPromotion promotion = db.EntityPromotions.FirstOrDefault(o => o.Show == "Show");
                        if (promotion != null)
                        {
                            model.promotion.Name = promotion.Name;
                            model.promotion.Description = promotion.Description;
                            model.promotion.Itinerary = promotion.Itinerary;
                            model.promotion.Note = promotion.Note;
                            //SEO
                            model.promotion.Titletag = promotion.Titletag;
                            model.promotion.Meta_description = promotion.Meta_description;
                            model.promotion.Meta_keyword = promotion.Meta_keyword;

                            foreach (var listPromotion in db.EntityPromotions.Where(o => o.Show == "Hidden").Take(4))
                            {
                                model.listPromotion.Add(new Promotion()
                                {
                                    Id = listPromotion.Id,
                                    Name = listPromotion.Name,
                                    Image = listPromotion.Image,
                                    Description = listPromotion.Description
                                });
                            }

                            //using loop to get all Style in EntityStyle
                            foreach (var style in db.EntityStyles)
                            {
                                model.listStyle.Add(new Style()
                                {
                                    Id = style.Id,
                                    Name = style.Name
                                });
                            }
                            model.category = new SelectList(model.listStyle.ToArray(), "Name", "Name");

                            return View(model);
                        }
                    return View(model);
                }
            }
            else
            {
                using (dbVietPremierDataContext db = new dbVietPremierDataContext())
                {
                    EntityPromotion promotion = db.EntityPromotions.FirstOrDefault(o => o.Show == "Show");
                    if (promotion != null)
                    {
                        model.promotion.Name = promotion.Name;
                        model.promotion.Description = promotion.Description;
                        model.promotion.Itinerary = promotion.Itinerary;
                        model.promotion.Note = promotion.Note;
                        //SEO
                        model.promotion.Titletag = promotion.Titletag;
                        model.promotion.Meta_description = promotion.Meta_description;
                        model.promotion.Meta_keyword = promotion.Meta_keyword;

                        foreach (var listPromotion in db.EntityPromotions.Where(o => o.Show == "Hidden").Take(4))
                        {
                            model.listPromotion.Add(new Promotion()
                            {
                                Id = listPromotion.Id,
                                Name = listPromotion.Name,
                                Image = listPromotion.Image,
                                Description = listPromotion.Description
                            });
                        }

                        //using loop to get all Style in EntityStyle
                        foreach (var style in db.EntityStyles)
                        {
                            model.listStyle.Add(new Style()
                            {
                                Id = style.Id,
                                Name = style.Name
                            });
                        }
                        model.category = new SelectList(model.listStyle.ToArray(), "Name", "Name");

                        TempData["ContactUs"] = "Error";

                        return View(model);
                    }
                    return View(model);
                }

            }
        }

        public ActionResult Detail(string title)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                int idPromotion = Utilities.GetIdByTitle(title, VietPremierMenu.Promotion, VietPremierDetailList.Detail);
                GetPromotionModel model = new GetPromotionModel();

                EntityPromotion promotion = db.EntityPromotions.FirstOrDefault(o => o.Id == idPromotion);
                if (promotion != null)
                {
                    model.promotion.Id = promotion.Id;
                    model.promotion.Name = promotion.Name;
                    model.promotion.Description = promotion.Description;
                    model.promotion.Itinerary = promotion.Itinerary;
                    model.promotion.Note = promotion.Note;
                    //SEO
                    model.promotion.Titletag = promotion.Titletag;
                    model.promotion.Meta_description = promotion.Meta_description;
                    model.promotion.Meta_keyword = promotion.Meta_keyword;

                    foreach (var listPromotion in db.EntityPromotions.Where(o => o.Id != idPromotion).Take(4))
                    {
                        model.listPromotion.Add(new Promotion()
                        {
                            Id = listPromotion.Id,
                            Name = listPromotion.Name,
                            Image = listPromotion.Image,
                            Description = listPromotion.Description
                        });
                    }
                    //using loop to get all Style in EntityStyle
                    foreach (var style in db.EntityStyles)
                    {
                        model.listStyle.Add(new Style()
                        {
                            Id = style.Id,
                            Name = style.Name
                        });
                    }
                    model.category = new SelectList(model.listStyle.ToArray(), "Name", "Name");

                    return View(model);
                }
                return View();
            }
        }

        [HttpPost]
        public ActionResult Detail(int idPromotion, GetPromotionModel model)
        {
            if (ModelState.IsValid)
            {
                using (dbVietPremierDataContext db = new dbVietPremierDataContext())
                {
                    StringBuilder mailBody = new StringBuilder();
                    mailBody.AppendFormat("<h1>Promotion Program Information</h1>");
                    mailBody.AppendFormat("<table><tr><td style='width:200px'>Promotion program :</td><td>" + model.promotion.Name + "</td></tr>");
                    mailBody.AppendFormat("<tr style='height:20px'></tr> ");
                    mailBody.AppendFormat("<tr><td>Họ tên:</td><td>" + model.Gender + " " + model.FullName + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Đơn vị/ Doanh nghiệp:</td><td>" + model.Supplier + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Nơi đến:</td><td>" + model.Destination + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Quốc tịch:</td><td>" + model.Nationality + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Điện thoại:</td><td>" + model.Telephone + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Email:</td><td>" + model.Email + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Số lượng người lớn:</td><td>" + model.NumberofAdults + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Số lượng trẻ em:</td><td>" + model.NumberofChildren + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Ngày đi:</td><td>" + model.DateFrom + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Ngày về:</td><td>" + model.DateTo + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Chuẩn khách sạn:</td><td>" + model.Place + "</td></tr>");
                    mailBody.AppendFormat("<tr><td>Yêu cầu đặc biệt khác:</td><td>" + model.SpecialRequest + "</td></tr></table>");
                    mailBody.AppendFormat("<br></br>");
                    mailBody.AppendFormat("<p>Email được gửi từ một email ảo, No reply</p>");

                    try
                    {
                        MailHelper.SendMailMessage("noreply@khainguyenepco.vn", "sales@vietnampremier.com", "hmduc@khainguyenepco.vn", "khoa@vietnampremier.com", "subject", mailBody.ToString(), "smtp.gmail.com", true, "noreply@khainguyenepco.vn", "kh@inguyen");
                        EntityFeedBack feedback = new EntityFeedBack();
                        feedback.Email = model.Email;
                        feedback.DateCreate = DateTime.Now;
                        feedback.Body = mailBody.ToString();
                        feedback.Page = "Promotion";
                        db.EntityFeedBacks.InsertOnSubmit(feedback);
                        db.SubmitChanges();

                        TempData["Message"] = "Success";
                    }
                    catch
                    {
                        TempData["Message"] = "ErrorMessage";
                    }

                    model = new GetPromotionModel();
                    EntityPromotion promotion = db.EntityPromotions.FirstOrDefault(o => o.Id == idPromotion);
                    if (promotion != null)
                    {
                        model.promotion.Id = promotion.Id;
                        model.promotion.Name = promotion.Name;
                        model.promotion.Description = promotion.Description;
                        model.promotion.Itinerary = promotion.Itinerary;
                        model.promotion.Note = promotion.Note;
                        //SEO
                        model.promotion.Titletag = promotion.Titletag;
                        model.promotion.Meta_description = promotion.Meta_description;
                        model.promotion.Meta_keyword = promotion.Meta_keyword;

                        foreach (var listPromotion in db.EntityPromotions.Where(o => o.Id != idPromotion).Take(4))
                        {
                            model.listPromotion.Add(new Promotion()
                            {
                                Id = listPromotion.Id,
                                Name = listPromotion.Name,
                                Image = listPromotion.Image,
                                Description = listPromotion.Description
                            });
                        }

                        //using loop to get all Style in EntityStyle
                        foreach (var style in db.EntityStyles)
                        {
                            model.listStyle.Add(new Style()
                            {
                                Id = style.Id,
                                Name = style.Name
                            });
                        }
                        model.category = new SelectList(model.listStyle.ToArray(), "Name", "Name");

                        return View(model);
                    }
                    return View(model);
                }
            }
            else
            {
                using (dbVietPremierDataContext db = new dbVietPremierDataContext())
                {
                    EntityPromotion promotion = db.EntityPromotions.FirstOrDefault(o => o.Id == idPromotion);
                    if (promotion != null)
                    {
                        model.promotion.Id = promotion.Id;
                        model.promotion.Name = promotion.Name;
                        model.promotion.Description = promotion.Description;
                        model.promotion.Itinerary = promotion.Itinerary;
                        model.promotion.Note = promotion.Note;
                        //SEO
                        model.promotion.Titletag = promotion.Titletag;
                        model.promotion.Meta_description = promotion.Meta_description;
                        model.promotion.Meta_keyword = promotion.Meta_keyword;

                        foreach (var listPromotion in db.EntityPromotions.Where(o => o.Id != idPromotion).Take(4))
                        {
                            model.listPromotion.Add(new Promotion()
                            {
                                Id = listPromotion.Id,
                                Name = listPromotion.Name,
                                Image = listPromotion.Image,
                                Description = listPromotion.Description
                            });
                        }

                        //using loop to get all Style in EntityStyle
                        foreach (var style in db.EntityStyles)
                        {
                            model.listStyle.Add(new Style()
                            {
                                Id = style.Id,
                                Name = style.Name
                            });
                        }
                        model.category = new SelectList(model.listStyle.ToArray(), "Name", "Name");

                        TempData["ContactUs"] = "Error";

                        return View(model);
                    }
                    return View(model);
                }

            }
        }

    }
}
